package controllers;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import models.city.City;
import models.game.Game;
import models.players.Player;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import utils.GameUtils;
import utils.GenericPlayerUtils;
import utils.PlayerUtils;

@Controller
public class GameActionsController {

	@RequestMapping("/CallAction")
	public synchronized String CallAction(HttpServletRequest request) {
		
		//Gets the action from the session and the option from the ActiveOptionsPage.jsp selection
		String action = request.getSession().getAttribute("action").toString();
		String option = request.getParameter("option").toString();
		Player player = (Player) request.getSession().getAttribute("player");
		Game game = (Game) request.getSession().getAttribute("game");

		//If the selected option is back, redirects to the active page
		if (option.equals("Back")) {
			return "ActivePage";
		}

		//Calls the method corresponding to the action with the selected option as a parameter
		switch (action) {
		case "Drive":
			drive(player, game, option);
			break;

		case "DirectFlight":
			directFlight(player, game, option);
			break;

		case "CharterFlight":
			charterFlight(player, game, option);
			break;

		case "ShuttleFlight":
			shuttleFlight(player, game, option);
			break;

		case "BuildResearchStation":
			buildResearchStation(player, game, option);
			break;

		case "TreatDisease":
			treatDisease(player, game, option);
			break;

		case "DiscoverACure":
			discoverACure(player, game, option);
			break;

		case "RecieveKnowledge":
			recieveKnowledge(player, game, option);
			break;

		case "ShareKnowledge":
			shareKnowledge(player, game, option);
			break;

		case "EndTurn":
			endTurn(player, game);
			break;

		default:
			break;
		}
		return "redirect:/CheckGameStatus";

	}
	
	// A method that counts down the number of actions and calls the changeTurn method when the player has done his last one
	private static void doAction(Player player, Game game) {
		int actions = game.getActions();
		System.out.println("action: " + actions);
		System.out.println("active action" + actions);
		if (actions == GameMenuController.getNumberOfActions()) {
			System.out.println("action: " + actions);
			System.out.println("active action" + actions);
			changeTurn(player, game);
		} else {
			actions++;
			game.setActions(actions);
		}
	}

	// Changes the active turn
	private static void changeTurn(Player player, Game game) {
		game.setActions(1);
		endTurnMethods(player, game);
		int activeTurn = game.getActiveTurn();
		if (activeTurn == GameMenuController.getNumberOfPlayers()) {
			game.setActiveTurn(1);
		} else {
			activeTurn++;
			game.setActiveTurn(activeTurn);
		}
	}

	// Calls methods that are required after the end of each turn
	private static void endTurnMethods(Player player, Game game) {
		
		// Resets the turn log
		game.setLog(new ArrayList<String>());
		
		// A city can outbreak only once per turn. This method resets the flag
		for (City city : game.getCities().values()) {
			city.setHasOutbreaked(false);
		}
		
		for (int i = 0; i < 2; i++) {
			GameUtils.drawPlayerCards(player, game);
		}
		for (int i = 0; i < 2; i++) {
			GameUtils.infect(game);
		}
	}

	// Now unnecessary and repetitive, to be refactored once roles are implemented
	public void drive(Player player, Game game, String string) {
		doAction(player, game);
		if (true) {
			GenericPlayerUtils.drive(player, game, string);
		}
	}

	
	public void directFlight(Player player, Game game, String string) {
		doAction(player, game);
		if (true) {
			GenericPlayerUtils.directFlight(player, game, string);
		}
	}

	public void charterFlight(Player player, Game game, String string) {
		doAction(player, game);
		if (true) {
			GenericPlayerUtils.charterFlight(player, game, string);
		}
	}

	public void shuttleFlight(Player player, Game game, String string) {
		doAction(player, game);
		if (true) {
			GenericPlayerUtils.shuttleFlight(player, game, string);
		}
	}

	public void buildResearchStation(Player player, Game game, String string) {
		doAction(player, game);
		if (true) {
			GenericPlayerUtils.buildResearchStation(player, game);
		}
	}

	public void treatDisease(Player player, Game game, String string) {
		doAction(player, game);
		if (true) {
			GenericPlayerUtils.treatDisease(player, game);
		}
	}

	public void discoverACure(Player player, Game game, String string) {
		doAction(player, game);
		if (true) {
			GenericPlayerUtils.discoverACure(player, game, string);
		}
	}

	public void recieveKnowledge(Player player, Game game, String string) {
		doAction(player, game);
		if (true) {
			GenericPlayerUtils.recieveKnowledge(player, game, string);
		}
	}

	public void shareKnowledge(Player player, Game game, String string) {
		doAction(player, game);
		if (true) {
			GenericPlayerUtils.shareKnowledge(player, game, string);
		}
	}

	public void endTurn(Player player, Game game) {
		changeTurn(player, game);
	}

}
